<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" destroy-on-close
        :before-close="closeDailog" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px" label-position="top">
            <el-form-item label="模板名称" prop="templateName">
                <el-input v-model="dataForm.templateName"></el-input>
            </el-form-item>
            <el-form-item label="模板内容" prop="templateContent">
                <el-input type="textarea" v-model="dataForm.templateContent" :autosize="{ minRows: 5, maxRows: 100}">
                </el-input>
            </el-form-item>
            <el-button type="primary" size="mini" @click="add">增行</el-button>
            <el-button type="danger" size="mini" @click="del">删行</el-button>
            <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column prop="paramName" label="参数名称">
                    <template slot-scope="scope">
                        <el-input size="small" v-model="scope.row.paramName" placeholder="请输入内容"
                            @change="handleEdit(scope.$index, scope.row)"></el-input>
                    </template>
                </el-table-column>
                <el-table-column prop="paramCode" label="参数编码">
                    <template slot-scope="scope">
                        <el-input size="small" v-model="scope.row.paramCode" placeholder="请输入内容"
                            @change="handleEdit(scope.$index, scope.row)"></el-input>
                    </template>
                </el-table-column>
                <el-table-column prop="paramValue" label="参数值">
                    <template slot-scope="scope">
                        <el-input size="small" v-model="scope.row.paramValue" placeholder="默认值"></el-input>
                    </template>
                </el-table-column>
            </el-table>

        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as emailtemplateService from '@/api/chat/emailtemplate'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkTemplate: 0,
                    templateName: '',
                    templateContent: ''
                },
                multipleSelection: [],
                tableData: [{
                    pkParam: '',
                    paramCode: '',
                    paramName: '',
                    paramValue: '',
                    t: new Date().getTime()
                }],
                dataRule: {
                    content: [{
                        required: true,
                        message: '模板不能为空',
                        trigger: 'blur'
                    }]
                }
            }
        },
        components: {},
        methods: {
            init(id) {
                this.dataForm.pkParam = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkParam) {
                        emailtemplateService.getemailtemplateinfo(this.dataForm.pkParam).then(
                            data => {
                                this.dataForm.pkTemplate = data.pkTemplate
                                this.dataForm.templateName = data.templateName
                                this.dataForm.templateContent = data.templateContent
                                this.tableData = data.paramDatas
                            })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkTemplate': this.dataForm.pkTemplate || undefined,
                            'templateName': this.dataForm.templateName,
                            'templateContent': this.dataForm.templateContent,
                            'paramDatas': this.tableData
                        }
                        var tick = !this.dataForm.pkTemplate ? emailtemplateService.addemailtemplate(params) :
                            emailtemplateService.updateemailtemplate(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            add() {
                this.tableData.push({
                    pkParam: '',
                    paramName: '',
                    paramCode: '',
                    paramValue: '',
                    t: new Date().getTime()
                })
            },
            del() {
                if (this.multipleSelection.length > 0) {
                    this.multipleSelection.forEach(item => {
                        if (item.t != '' && item.t > 0) {
                            this.tableData.forEach((c, ci) => {
                                if (item.t == c.t) {
                                    this.tableData.splice(ci, 1);
                                }
                            })
                        }
                    })
                }
            },
            handleEdit(index, row) {
                console.log(index, row);
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>